package com.freeman.cement.controller;

import com.freeman.cement.annotation.NeedRole;
import com.freeman.cement.entity.ResponseResult;
import com.freeman.cement.exception.AppException;
import com.freeman.cement.exception.AppExceptionCodeMsg;
import com.freeman.cement.service.MessageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * 值班室消息查询
 */
@RestController
@RequestMapping("/message")
@CrossOrigin    //允许跨域访问
public class MessageController {
    @Resource
    private MessageService service;

    /**
     * 根据接收者信息（值班室id），分页查询消息
     * @param dutyRoomId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @NeedRole("管理员||值班员")
    @RequestMapping(value = "/selectMessage", method = RequestMethod.POST)
    public ResponseResult selectMessage(@RequestParam int dutyRoomId, @RequestParam int pageNo, @RequestParam int pageSize) {
        if (pageNo > 0 && pageSize <= 1000) {
            return service.selectMessage(dutyRoomId, pageNo, pageSize);
        } else {
            throw new AppException(AppExceptionCodeMsg.PAGE_NO_OR_SIZE_WRONG);
        }
    }

    /**
     * 值班室读取未读消息
     * 目前供监控页面消息块使用
     * @值班室id dutyRoomId
     * @读取消息的条数 count
     * @读取该时间点之后的消息 after
     * @return
     */
    @NeedRole("管理员||值班员")
    @RequestMapping(value = "/selectUnreadMessage", method = RequestMethod.POST)
    public ResponseResult selectUnreadMessage(@RequestParam int dutyRoomId, @RequestParam int count, String after) {
        if (count > 0 && count <= 1000) {
            return service.selectUnreadMessage(dutyRoomId, count, after);
        } else {
            throw new AppException(51236, "读取条目应小于1000条");
        }
    }

    /**
     * 记录消息读取次数，调用一次该接口，所读取消息次数加1
     * @param id
     * @return
     */
    @RequestMapping(value = "/readTimesAddOne", method = RequestMethod.POST)
    public ResponseResult readTimesAddOne(@RequestParam int id) {
        return service.readTimesAddOne(id);
    }

}

